package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclycsjbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclycsjbgBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclycsjbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclycsjbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclycsjbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料延迟时间试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclycsjbgBsInfoServiceImpl implements IWdclycsjbgBsInfoService {

    private final WdclycsjbgBsInfoMapper baseMapper;

    /**
     * 查询无机结合料稳定材料延迟时间试验检测报告
     */
    @Override
    @FIllBasicInfo
    public WdclycsjbgBsInfoVo queryById(Long excelId){
        WdclycsjbgBsInfoVo wdclycsjbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(wdclycsjbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(wdclycsjbgBsInfoVo.getJiancjl())) {
            return wdclycsjbgBsInfoVo;
        }
        return jcjl(wdclycsjbgBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料延迟时间试验检测报告列表
     */
    @Override
    public TableDataInfo<WdclycsjbgBsInfoVo> queryPageList(WdclycsjbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclycsjbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclycsjbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料延迟时间试验检测报告列表
     */
    @Override
    public List<WdclycsjbgBsInfoVo> queryList(WdclycsjbgBsInfoBo bo) {
        LambdaQueryWrapper<WdclycsjbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclycsjbgBsInfo> buildQueryWrapper(WdclycsjbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclycsjbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclycsjbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclycsjbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), WdclycsjbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclycsjbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclycsjbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclycsjbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclycsjbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclycsjbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclycsjbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclycsjbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), WdclycsjbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclycsjbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclycsjbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclycsjbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclycsjbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclycsjbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), WdclycsjbgBsInfo::getQuywz, bo.getQuywz());
//        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), WdclycsjbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), WdclycsjbgBsInfo::getTuycd, bo.getTuycd());
//        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), WdclycsjbgBsInfo::getDaibsl, bo.getDaibsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclycsjbgBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), WdclycsjbgBsInfo::getJiancjl, bo.getJiancjl());
//        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), WdclycsjbgBsInfo::getShigwtdw, bo.getShigwtdw());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclycsjbgBsInfo::getGongcmc, bo.getGongcmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclycsjbgBsInfo::getShebIds, bo.getShebIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), WdclycsjbgBsInfo::getBgFlag, bo.getBgFlag());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclycsjbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclycsjbgBsInfo::getPandyjIds, bo.getPandyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getJilzc()), WdclycsjbgBsInfo::getJilzc, bo.getJilzc());
//        lqw.eq(StringUtils.isNotBlank(bo.getWendjzl()), WdclycsjbgBsInfo::getWendjzl, bo.getWendjzl());
//        lqw.eq(bo.getJiehljl() != null, WdclycsjbgBsInfo::getJiehljl, bo.getJiehljl());
//        lqw.eq(bo.getYancsj0() != null, WdclycsjbgBsInfo::getYancsj0, bo.getYancsj0());
//        lqw.eq(bo.getYancsj1() != null, WdclycsjbgBsInfo::getYancsj1, bo.getYancsj1());
//        lqw.eq(bo.getYancsj2() != null, WdclycsjbgBsInfo::getYancsj2, bo.getYancsj2());
//        lqw.eq(bo.getYancsj3() != null, WdclycsjbgBsInfo::getYancsj3, bo.getYancsj3());
//        lqw.eq(bo.getDaibz0() != null, WdclycsjbgBsInfo::getDaibz0, bo.getDaibz0());
//        lqw.eq(bo.getDaibz1() != null, WdclycsjbgBsInfo::getDaibz1, bo.getDaibz1());
//        lqw.eq(bo.getDaibz2() != null, WdclycsjbgBsInfo::getDaibz2, bo.getDaibz2());
//        lqw.eq(bo.getDaibz3() != null, WdclycsjbgBsInfo::getDaibz3, bo.getDaibz3());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料延迟时间试验检测报告
     */
    @Override
    public Boolean insertByBo(WdclycsjbgBsInfoBo bo) {
        WdclycsjbgBsInfo add = BeanUtil.toBean(bo, WdclycsjbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料延迟时间试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(WdclycsjbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        WdclycsjbgBsInfo update = BeanUtil.toBean(bo, WdclycsjbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(WdclycsjbgBsInfoBo bo) {
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclycsjbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料延迟时间试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 机结合料稳定材料延迟时间试验检测报告 检测结论
     */
    @Override
    public WdclycsjbgBsInfoVo jcjl(WdclycsjbgBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：该样品所检参数结果为无侧限抗压强度与延迟时间标准曲线，该混合料最大允许延迟时间为3h。");
        return vo;
    }
}
